﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
/// <summary>
/// des + base64 加密解密工具
/// </summary>
public class DESCrypto  {
    public static string  Encrypt(string toEncrypt,string sKey,string sIv )
    {
        byte[] toencrypt = Encoding.UTF8.GetBytes(toEncrypt);
        byte[] key = Encoding.UTF8.GetBytes(sKey.Substring(0,8));
        byte[] iv = Encoding.UTF8.GetBytes(sIv.Substring(0,8));
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            des.Key = key;
            des.IV = iv;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(),CryptoStreamMode.Write))
                {
                    cryptoStream.Write(toencrypt,0,toencrypt.Length);
                    cryptoStream.FlushFinalBlock();
                    cryptoStream.Close();
                    return Convert.ToBase64String(memoryStream.ToArray());
                }
            }
        }
    }

    public static string GenerateKey()
    {
        using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider())
        {
            return Encoding.UTF8.GetString(desCryptoServiceProvider.Key);
        }
    }
    public static string GenerateIV()
    {
        using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider())
        {
            return Encoding.UTF8.GetString(desCryptoServiceProvider.IV);
        }
    }

    public static string  Decrypt(string toEncrypt,string sKey,string sIv )
    {
        byte[] toencrypt =Convert.FromBase64String(toEncrypt);
        byte[] key = Encoding.UTF8.GetBytes(sKey.Substring(0,8));
        byte[] iv = Encoding.UTF8.GetBytes(sIv.Substring(0,8));
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            des.Key = key;
            des.IV = iv;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateDecryptor(),CryptoStreamMode.Write))
                {
                    cryptoStream.Write(toencrypt,0,toencrypt.Length);
                    cryptoStream.FlushFinalBlock();
                    cryptoStream.Close();
                    return Encoding.UTF8.GetString( memoryStream.ToArray());
                }
            }
        }
    }

    public static void EncryptFile(string infile, string outfile, string key, string iv)
    {
        byte[] keyarr = Encoding.UTF8.GetBytes(key.Substring(0,8));
        byte[] ivarr = Encoding.UTF8.GetBytes(iv.Substring(0, 8));
        using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider())
        {
            desCryptoServiceProvider.Key = keyarr;
            desCryptoServiceProvider.IV = ivarr;
            if (!File.Exists(infile))
            {
                Debug.LogError("要加密的文件不存在");
                return;
            }
            if (File.Exists(outfile))
            {
                File.Delete(outfile);
            }
            using (FileStream infiFileStream = new FileStream(infile,FileMode.Open,FileAccess.Read))
            {
                using (FileStream outfiFileStream = new FileStream(outfile,FileMode.Create,FileAccess.Write))
                {
                    outfiFileStream.SetLength(0);
                    byte[] buffer = new byte[100];
                    long total = infiFileStream.Length;
                    long rlength = 0;
                    int length;
                    using (CryptoStream cryptoStream = new CryptoStream(outfiFileStream,
                        desCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        while (rlength<total)
                        {
                            length = infiFileStream.Read(buffer, 0, buffer.Length);
                            rlength += length;
                            cryptoStream.Write(buffer,0,length);
                        }
                    }
                }
            }
        }
    }

    public static void DecryptFile(string infile, string outfile, string key, string iv)
    {
        byte[] keyarr = Encoding.UTF8.GetBytes(key.Substring(0,8));
        byte[] ivarr = Encoding.UTF8.GetBytes(iv.Substring(0, 8));
        using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider())
        {
            desCryptoServiceProvider.Key = keyarr;
            desCryptoServiceProvider.IV = ivarr;
            if (!File.Exists(infile))
            {
                Debug.LogError("要解密的文件不存在");
                return;
            }
            if (File.Exists(outfile))
            {
                File.Delete(outfile);
            }
            using (FileStream infiFileStream = new FileStream(infile,FileMode.Open,FileAccess.Read))
            {
                using (FileStream outfiFileStream = new FileStream(outfile,FileMode.Create,FileAccess.Write))
                {
                    outfiFileStream.SetLength(0);
                    byte[] buffer = new byte[100];
                    long total = infiFileStream.Length;
                    long rlength = 0;
                    int length;
                    using (CryptoStream cryptoStream = new CryptoStream(outfiFileStream,
                        desCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        while (rlength<total)
                        {
                            length = infiFileStream.Read(buffer, 0, buffer.Length);
                            rlength += length;
                            cryptoStream.Write(buffer,0,length);
                        }
                    }
                }
            }
        }
    }
}
